package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DbOrderPoints;
import com.ruoyi.system.domain.Shangpinxinxi;
import com.ruoyi.system.mapper.ShangpinxinxiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DbPointsMapper;
import com.ruoyi.system.domain.DbPoints;
import com.ruoyi.system.service.IDbPointsService;

/**
 * 积分Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-22
 */
@Service
public class DbPointsServiceImpl implements IDbPointsService 
{
    @Autowired
    private DbPointsMapper dbPointsMapper;

    @Autowired
    private ShangpinxinxiMapper shangpinxinxiMapper;

    /**
     * 查询积分
     * 
     * @param id 积分主键
     * @return 积分
     */
    @Override
    public DbPoints selectDbPointsById(Long id)
    {
        return dbPointsMapper.selectDbPointsById(id);
    }

    /**
     * 查询积分列表
     * 
     * @param dbPoints 积分
     * @return 积分
     */
    @Override
    public List<DbPoints> selectDbPointsList(DbPoints dbPoints)
    {
        return dbPointsMapper.selectDbPointsList(dbPoints);
    }

    @Override
    public int addDbPoints(DbOrderPoints dbOrderPoints) {
        int result = 1;
        try{
            Long shangpinId = dbOrderPoints.getShangpinId();
            //单价
            Integer orderPrice = dbOrderPoints.getOrderPrice();
            //类型
            String jfType = dbOrderPoints.getJfType();
            //数量
            Integer orderNum = dbOrderPoints.getOrderNum();
            //获取商品信息
            Shangpinxinxi shangpinxinxi = shangpinxinxiMapper.selectShangpinxinxiById(shangpinId);
            //获取农户id
            String nonghuZhangHao = shangpinxinxi.getNonghuzhanghao();
            Long nonghuId = Long.valueOf(nonghuZhangHao);
            String nonghuXingm = shangpinxinxi.getNonghuxingming();
            //查询积分表是否有信息
            DbPoints search = new DbPoints();
            search.setUserId(nonghuId);
            List<DbPoints> listNonghu = dbPointsMapper.selectDbPointsList(search);
//            DbPoints nonghuPo = dbPointsMapper.selectDbPointsById(nonghuId);
            //若有信息直接加积分
            System.out.println("listNonghu:" + orderPrice+orderNum);
            if (listNonghu != null && listNonghu.size() > 0 ){
                DbPoints nonghuPo = listNonghu.get(0);
                nonghuPo.setUpdateTime(DateUtils.getNowDate());
                nonghuPo.setPointsValue(nonghuPo.getPointsValue()+orderPrice*orderNum);
                result = dbPointsMapper.updateDbPoints(nonghuPo);
            }else {
                //不存在代表第一次添加
                DbPoints dbPoints = new DbPoints();
                dbPoints.setUserId(nonghuId);
                dbPoints.setUserName(nonghuXingm);
                dbPoints.setPointsValue(Long.valueOf(orderPrice*orderNum));
                dbPoints.setJfType(jfType);
                dbPoints.setCreateTime(DateUtils.getNowDate());
                dbPointsMapper.insertDbPoints(dbPoints);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 新增积分
     * 
     * @param dbPoints 积分
     * @return 结果
     */
    @Override
    public int insertDbPoints(DbPoints dbPoints)
    {
        dbPoints.setCreateTime(DateUtils.getNowDate());
        return dbPointsMapper.insertDbPoints(dbPoints);
    }

    /**
     * 修改积分
     * 
     * @param dbPoints 积分
     * @return 结果
     */
    @Override
    public int updateDbPoints(DbPoints dbPoints)
    {
        dbPoints.setUpdateTime(DateUtils.getNowDate());
        return dbPointsMapper.updateDbPoints(dbPoints);
    }

    /**
     * 批量删除积分
     * 
     * @param ids 需要删除的积分主键
     * @return 结果
     */
    @Override
    public int deleteDbPointsByIds(Long[] ids)
    {
        return dbPointsMapper.deleteDbPointsByIds(ids);
    }

    /**
     * 删除积分信息
     * 
     * @param id 积分主键
     * @return 结果
     */
    @Override
    public int deleteDbPointsById(Long id)
    {
        return dbPointsMapper.deleteDbPointsById(id);
    }
}
